
import {
  asyncSetInterval, notifications, zeroReportFail, fixZeroInfo
} from '../../lib/zeroReportUtils.js';
import { xqykjzz } from './cwbbData.js';

// 确定按钮
let sureBtn = async (ele, isSure) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      ele = ele
      || $(
        '.mini-messagebox.mini-panel.mini-window.mini-window-drag .mini-messagebox-buttons .mini-button-text'
      );
      if (ele.length) {
        if (isSure) {
          ele[0].click();
        } else if ($('.mini-messagebox .mini-messagebox-table').text().includes('请先进行财务会计制度及核算软件备案')) {
          notifications({ message: '请先进行财务会计制度及核算软件备案' });
          setTimeout(() => {
            zeroReportFail({
              success: false,
              message: '请先进行财务会计制度及核算软件备案'
            });
          }, 2000);
        } else {
          Array.from(ele).forEach((item) => {
            item.click();
          });
        }
      }
      resolve(true);
    }, 1000);
  });
};
const cwbbReport = async ({ cwbbInfo, customerType, recordList }) => {
  let lis = null;
  // return true;
  return asyncSetInterval(() => {
    let ok = $('#sbny-ok');
    if (ok.length) {
      ok.click();
      return true;
    }
  }, 1000).then(async () => {
    // let number = 0;
    await sureBtn();
    await sureBtn();
    await sureBtn();
    return true;
  }).then(() =>{
    return asyncSetInterval(() => {
      lis = $('#tabs > table > tbody > tr > td > div.mini-tabs-scrollCt > div.mini-tabs-headers > table > tbody > tr > td.mini-tab');
      if (lis.length) {
        lis = Array.from(lis);
        return true;
      }
    }, 1000);
  }).then(() => {
    let zcfzbBtn = lis.find(item => $(item).text().includes('资产负债表'));
    zcfzbBtn.click();
    let systemData = false;
    let shuijuData = false;
    return asyncSetInterval(async () => {
      let zcfzb = cwbbInfo.zcfzb;
      let table = $('#table_001');
      let zcfzbDefault = customerType.includes('小企业会计准则')
        ? xqykjzz.zcfzb
        : '';
      if (table.length) {
        let result = Object.keys(zcfzbDefault).map((key) => {
          let col = zcfzbDefault[key];
          let colValue = zcfzb[col];
          let value = key.includes('C') || key.includes('G') ? colValue.qmye : colValue.ncye;
          // let value = key.includes('C1') || key.includes('G1') ? 1 : 2;
          // let value = 1;
          if (parseFloat(value)) systemData = true;
          if (parseFloat($(`#${key}`).val())) shuijuData = true;
          // $(`#${key}`).val(value || 0.0);
          return {
            key,
            value
          };
        });
        if (systemData) {
          result.forEach(({ key, value }) => {
            $(`#${key}`).val(value || 0.0);
          });
        } else if (!shuijuData && !systemData) {
          let arr = ['001C9', '001D9', '001H28', '001G28'];
          arr.forEach((item) => {
            $(`#${item}`).val(0.01);
            $(`#${item}`).blur();
          });
        }
        $('#sb_tempSaveCheck').click();
        await sureBtn();
        await sureBtn();
        return true;
      }
    }, 1000);
  })
    .then(() => {
      let lrbBtn = lis.find(item => $(item).text().includes('利润表'));
      lrbBtn.click();
      return asyncSetInterval(async () => {
        let lrb = cwbbInfo.lrb;
        let lrbDefault = customerType.includes('小企业会计准则') ? xqykjzz.lrb : '';
        let result = Object.keys(lrbDefault).map((key) => {
          let col = lrbDefault[key];
          let colValue = lrb[col];
          let value = key.includes('G') ? colValue.bnljje : colValue.bqje;
          $(`#${key}`).val(value || '0.00');
          return {
            key,
            value
          };
        });
        console.log(result);
        $('#sb_tempSaveCheck').click();
        await sureBtn();
        await sureBtn();
        return true;
      }, 1000);
    })
    .then(() => {
      let xjllbBtn = lis.find(item => $(item).text().includes('现金流量表'));
      xjllbBtn.click();
      return asyncSetInterval(async () => {
        let xjllb = cwbbInfo.xjllb;
        let xjllbDefault = customerType.includes('小企业会计准则')
          ? xqykjzz.xjllb
          : '';
        let result = Object.keys(xjllbDefault).map((key) => {
          let col = xjllbDefault[key];
          let colValue = xjllb[col];
          let value = key.includes('G') ? colValue.bnljje : colValue.bqje;
          $(`#${key}`).val(value || '0.00');
          return {
            key,
            value
          };
        });
        $('#sb_tempSaveCheck').click();
        await sureBtn();
      }, 1000).then(() => {
        let num = 0;
        return asyncSetInterval(() => {
          let btn = $('.mini-messagebox .mini-button');
          if (btn.length) {
            btn.click();
            return true;
          }
          if (num > 1) {
            return true;
          }
          num++;
        }, 2000);
      });
    })
    .then(() => {
      return asyncSetInterval(async () => {
        document.querySelector('#sb_save').click();
        let cwbb = recordList.find(item => item.includes('财务报告报送与信息采集'));
        cwbb.isSuccess = true;
        fixZeroInfo({ recordList: [cwbb] });
        await sureBtn('', true);
      }, 1000);
    });
};

export {
  cwbbReport
};
